package com.blb.controller;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Brand;
import com.blb.entity.Commodity;
import com.blb.entity.Comtype;
import com.blb.mapper.ComtypeMapper;
import com.blb.service.IBrandService;
import com.blb.service.ICommodityService;
import com.blb.utils.LayuiPage;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuzhuo
 * @since 2019-07-11
 */
@Controller
@RequestMapping("commodity")
public class CommodityController {
	@Autowired
	private ICommodityService commodityService;
	@Autowired
	private ComtypeMapper comtypeMapper;
	@Autowired
	private IBrandService brandService;
	
	@RequestMapping("index")
	public String getIndex(){
		return "admin/commodity/commodity_list";
	}
	
	/**
	 * 分页
	 * @param page
	 * @param limit
	 * @param name
	 * @return
	 */
	@RequestMapping("page")
	@ResponseBody
	public LayuiPage page(int page,int limit,String name){
		Page<Commodity> page2 = new Page<Commodity>(page,limit);
		IPage<Commodity> ipage = null;
		ipage = commodityService.page(page2, null);
		
		if(name!=null && !name.equals("")){
			ipage = commodityService.page(page2, new QueryWrapper<Commodity>().like("name", name));
		}
		LayuiPage layuiPage = new LayuiPage();
		layuiPage.setCount((int) ipage.getCurrent());
		layuiPage.setData(ipage.getRecords());
		return layuiPage;
	}
	
	/**
	 * 展示添加页面
	 * @param model
	 * @param brand
	 * @return
	 */
	@RequestMapping("showAdd")
	public String showAdd(Model model,Brand brand){
		List<Comtype> comtypes = comtypeMapper.selectByExample(null);
		model.addAttribute("comtypes", comtypes);
		List<Brand> brands = brandService.getBrand(null);
		model.addAttribute("brands", brands);
		return "admin/commodity/commodity_add";
	}
	
	/**
	 * 添加
	 * @param commodity
	 * @param comtypes
	 * @param dimg
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public String add(Commodity commodity,@Param("comtypes")String comtypes,@Param("img")String dimg){
		try {
			commodityService.add(commodity, comtypes, dimg);
		} catch (Exception e) {
			e.printStackTrace();
			e.getMessage();
		}
		return "";
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String delete(@RequestBody List<String> ids){
		try {
			commodityService.delete(ids);
		} catch (Exception e) {
			e.printStackTrace();
			e.getMessage();
		}
		return "";
	}
	
	@RequestMapping("showUpdate")
	public String showUpdate(Model model){
		List<Comtype> list = comtypeMapper.selectByExample(null);
		model.addAttribute("comtypes", list);
		
		
		return "admin/commodity/commodity_update";
	}
}

